<?php

defined('SYSPATH') or die('No direct access allowed.');

/**
 * 分类管理
 *
 * @author liuxudong
 */
class Controller_Admin_Uploadify extends Controller {

    //处理图片上传
    public function action_images() {
        $prve_imagefiles = array();
        $table = $this->request->param('id');
        if (!empty($_FILES)) {
            $tempFile = $_FILES['Filedata']['tmp_name'];
            $new_targetPath = $_REQUEST['folder'] . '/' . date('Y', time()) . '/' . date('m', time()) . '/';
            $targetPath = $_SERVER['DOCUMENT_ROOT'] . $new_targetPath;
            $flag = $this->directory($targetPath);
            $news_filename = $this->random_filename($_FILES['Filedata']['name']);
            $targetFile = str_replace('//', '/', $targetPath) . $news_filename;
            $table_arr = explode('--', $table);
            $all_path = $new_targetPath . $news_filename;
            if (isset($_REQUEST['PHPSESSID'])) {
                Session::instance(NULL, $_REQUEST['PHPSESSID']);
            }
            if (isset($table_arr[1])) {
                $newstypefiles = ORM::factory($table_arr[0], $table_arr[1])->where('chr_filestype', '=', $table_arr[2]);
                $newstypefiles->chr_filepath = $new_targetPath . $news_filename;
                $newstypefiles->chr_createdate = time();
                $newstypefiles->save();
            } else {
                if (Session::instance()->get('imagesfile_define')) {
                    Session::instance()->delete('imagesfile_define');
                }
                if (Session::instance()->get('temp_imagefiles')) {
                   $temp_array = array($all_path);
                   $prve_imagefiles = array_merge(Session::instance()->get('temp_imagefiles'), $temp_array);
                    Session::instance()->set('temp_imagefiles', $prve_imagefiles);
                } else {
                    Session::instance()->set('temp_imagefiles', $all_path);
                    $prve_imagefiles[] = Session::instance()->get('temp_imagefiles');
                    Session::instance()->set('temp_imagefiles', $prve_imagefiles);
                }
                Session::instance()->set('imagesfile_define', $prve_imagefiles);
            }
            move_uploaded_file($tempFile, iconv("UTF-8", "gb2312", $targetFile));
            echo str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetFile);
        }
    }

    //生成文件路径
    public function directory($dir) {
        return is_dir($dir) or ($this->directory(dirname($dir)) and mkdir($dir, 0777));
    }

    //生成文件名字
    public function random_filename($filename) {
        $getExt = explode('.', $filename);
        $file_ext = $getExt[count($getExt) - 1];
        $new_filename = time() . rand(0, 99999999999) . "." . $file_ext;
        return $new_filename;
    }
    
    public function action_removeimage() {
        $imageid = $_POST['id'];
        $imagetable = $_POST['table'];
        $images = ORM::factory($imagetable, $imageid);
        
        if ($images->loaded()) {
            $images->delete($imageid);
            
            echo 'true';
        }
    }
    
    public function action_abc() {
        echo 'true';
    }

}